import schedule
import time
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

def fill_form():
    # 初始化 Selenium WebDriver
    options = webdriver.ChromeOptions()
    options.add_argument("--disable-gpu")  # 可选：减少资源消耗
    driver = webdriver.Chrome(options=options)

    try:
        # 打开问卷页面
        url = "https://www.wjx.top/vm/tope34P.aspx"
        driver.get(url)

        # 等待页面加载完成
        time.sleep(1)

        # 找到并点击“立即报名”按钮
        signup_button = driver.find_element(By.CSS_SELECTOR, '#slideChunk .slideChunkWord')
        ActionChains(driver).move_to_element(signup_button).click().perform()
        print("点击了立即报名按钮")

        # 等待页面加载表单
        time.sleep(1)

        # 填写表单的字段，例如学号和姓名
        student_id = driver.find_element(By.XPATH, '//*[@id="q1"]')
        student_id.send_keys("2021123456")

        name_field = driver.find_element(By.XPATH, '//*[@id="q2"]')
        name_field.send_keys("张三")

        # 提交表单
        submit_button = driver.find_element(By.XPATH, '//*[@id="ctlNext"]')
        ActionChains(driver).move_to_element(submit_button).click().perform()
        print("表单已提交")

        # 等待提交结果
        time.sleep(1)

    finally:
        # 关闭浏览器
        driver.quit()

# 设置定时任务，晚上 8 点 50 执行
schedule.every().day.at("20:47").do(fill_form)

# print("等待执行任务...")

while True:
    # 运行所有设置的定时任务
    schedule.run_pending()
    # 延迟一秒钟检查一次
    time.sleep(1)
